package coreClassLibrary;

import java.io.*;

public class IOStreamDemo {
    public static void main(String[] args) {
        // 1. 文件基本操作示例
        // fileBasicOperations();
        
        // // 2. 字节流操作示例
        byteStreamOperations();
        
        // // 3. 字符流操作示例
        // characterStreamOperations();
        
        // // 4. 缓冲流操作示例
        // bufferedStreamOperations();
    }

    // 文件基本操作
    private static void fileBasicOperations() {
        try {
            // 先创建目录
            File directory = new File("src/coreClassLibrary/Files");
            directory.mkdirs();

            // 然后在该目录下创建文件
            File file = new File("test.txt");
            
            // 创建新文件
            boolean created = file.createNewFile();
            System.out.println("文件创建: " + created);
            
            // 获取文件信息
            System.out.println("文件名: " + file.getName());
            System.out.println("文件路径: " + file.getAbsolutePath());
            System.out.println("文件大小: " + file.length());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 字节流操作
    private static void byteStreamOperations() {
        try (FileOutputStream fos = new FileOutputStream("byte.txt");
             FileInputStream fis = new FileInputStream("byte.txt")) {
            
            // 写入字节数据
            String data = "Hello, 字节流!";
            fos.write(data.getBytes());
            
            // 读取字节数据
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) != -1) {
                System.out.println("读取的内容: " + new String(buffer, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 字符流操作
    private static void characterStreamOperations() {
        try (FileWriter writer = new FileWriter("char.txt");
             FileReader reader = new FileReader("char.txt")) {
            
            // 写入字符数据
            writer.write("Hello, 字符流!");
            writer.flush();
            
            // 读取字符数据
            char[] buffer = new char[1024];
            int length;
            while ((length = reader.read(buffer)) != -1) {
                System.out.println("读取的内容: " + new String(buffer, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 缓冲流操作
    private static void bufferedStreamOperations() {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("buffered.txt"));
             BufferedReader reader = new BufferedReader(new FileReader("buffered.txt"))) {
            
            // 使用缓冲写入
            writer.write("Hello, 缓冲流!");
            writer.newLine(); // 写入一个换行符
            writer.write("第二行");
            writer.flush();
            
            // 使用缓冲读取
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("读取的行: " + line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 